#/bin/bash
RESOURCE="/home/gcc"

# 安装依赖的包
rpm -q wget &>/dev/null
if [ $? -ne 0 ]; then
    yum install wget -y
fi
rpm -q gcc &>/dev/null
if [ $? -ne 0 ]; then
    yum install gcc -y
fi
rpm -q gcc-c++ &>/dev/null
if [ $? -ne 0 ]; then
    yum install gcc-c++ -y
fi
rpm -q automake &>/dev/null
if [ $? -ne 0 ]; then
    yum install automake -y
fi

# 安装gmp
function buildGmp() {
    cd "${RESOURCE}/gmp-5.0.0"
    ./configure -prefix=/usr/local/gmp-5.0.0
    make -j4 && make install
    if [ $? -eq 0 ]; then
        echo "gmp安装成功"
    else
        echo "gmp安装失败"
        exit 1
    fi
}

if [ ! -f "${RESOURCE}/gmp-5.0.0.tar.gz" ]; then
    echo "没有gmp的资源"
    exit 1
else
    if [ ! -d "${RESOURCE}/gmp-5.0.0" ]; then
        cd ${RESOURCE}
        tar -zxvf gmp-5.0.0.tar.gz
    fi
    buildGmp
fi

# 安装mpfr
function buildMpfr() {
    cd "${RESOURCE}/mpfr-4.1.0"
    ./configure --prefix=/usr/local/mpfr-4.1.0 --with-gmp=/usr/local/gmp-5.0.0
    make -j4 && make install
    if [ $? -eq 0 ]; then
        echo "mpfr安装成功"
        #把mpfr lib 加入 ld.so.conf 不然gcc 编译报错
        echo /usr/local/mpfr-4.1.0/lib >>/etc/ld.so.conf
        ldconfig
    else
        echo "mpfr安装失败"
        exit 1
    fi
}
if [ ! -f "${RESOURCE}/mpfr-4.1.0.tar.gz" ]; then
    echo "没有mpfr-4.1.0.tar.gz资源"
    exit 1
else
    if [ ! -d "${RESOURCE}/mpfr-4.1.0" ]; then
        cd ${RESOURCE}
        tar -zxvf mpfr-4.1.0.tar.gz
    fi
    buildMpfr
fi

# # 安装mpc

function buildMpc() {
    cd "${RESOURCE}/mpc-1.2.1"
    ./configure --prefix=/usr/local/mpc-1.2.1 --with-gmp=/usr/local/gmp-5.0.0 --with-mpfr=/usr/local/mpfr-4.1.0
    make -j4 && make install
    if [ $? -eq 0 ]; then
        echo "mpc安装成功"
        # 创建软链接
        ln -s /usr/local/mpc-1.2.1/lib/libmpc.so.3.2.1 /lib64/libmpc.so.3
        ln -s /usr/local/mpc-1.2.1/lib/libmpc.so.3.2.1 /lib/libmpc.so.3
        ln -s /usr/local/mpc-1.2.1/lib/libmpc.so.3.2.1 /usr/lib64/libmpc.so.3
        ln -s /usr/local/mpc-1.2.1/lib/libmpc.so.3.2.1 /usr/lib/libmpc.so.3
        ln -s /usr/local/mpc-1.2.1/lib/libmpc.so.3.2.1 /usr/local/lib64/libmpc.so.3
        ln -s /usr/local/mpc-1.2.1/lib/libmpc.so.3.2.1 /usr/local/lib/libmpc.so.3
    else
        echo "mpc安装失败"
        exit 1
    fi
}
if [ ! -f "${RESOURCE}/mpc-1.2.1.tar.gz" ]; then
    echo "没有mpc-1.2.1.tar.gz资源"
    exit 1
else
    if [ ! -d "${RESOURCE}/mpc-1.2.1" ]; then
        cd ${RESOURCE}
        tar -zxvf mpc-1.2.1.tar.gz
    fi
    buildMpc
fi

# 配置环境变量
MPC_HOME=/usr/local/mpc-1.2.1
GMP_HOME=/usr/local/gmp-5.0.0
MPFR_HOME=/usr/local/mpfr-4.1.0
LD_LIBRARY_PATH=$MPC_LIB_HOME/lib:$GMP_HOME/lib:$MPFR_HOME/lib:$LD_LIBRARY_PATH
export MPC_LIB_HOME GMP_HOME MPFR_HOME LD_LIBRARY_PATH
# 重新刷新文件
source /etc/profile

# ## 安装gcc

function buildGcc() {
    cd "${RESOURCE}/gcc-5.3.0"
    ./configure --prefix=/usr/local/gcc-5.3.0 -enable-threads=posix -disable-checking -disable-multilib -enable-languages=c,c++ --with-gmp=/usr/local/gmp-5.0.0/ --with-mpfr=/usr/local/mpfr-4.1.0/ --with-mpc=/usr/local/mpc-1.2.1/ -with-tune=generic --with-arch_32=x86-64
    make -j4 && make install
    if [ $? -eq 0 ]; then
        echo "gcc安装成功"
        #备份(gcc失败的话就给还原,mv的文件反过来,如mv /usr/bin/gcc.old /usr/bin/gcc )
        mv /usr/bin/gcc /usr/bin/gcc.old
        mv /usr/bin/g++ /usr/bin/g++.old
        mv /usr/bin/c++ /usr/bin/c++.old
        mv /usr/bin/cpp /usr/bin/cpp.old
        mv /usr/bin/gcov /usr/bin/gcov.old
        ######创建配置文件
        # 创建gcc执行的软链接
        ln -sf /usr/local/gcc-5.3.0/bin/* /usr/bin/
        #　配置ld.so.conf文件
        echo /usr/local/gcc-5.3.0/lib64 >>/etc/ld.so.conf
        ldconfig
        # 复制libstdc++.so.6.0.21文件到/lib64/目录
        cp /usr/local/gcc-5.3.0/lib64/libstdc++.so.6.0.21 /lib64/
        # 创建软链 libstdc++.so.6
        cd /lib64
        ln -sf libstdc++.so.6.0.21 libstdc++.so.6
    else
        echo "gcc安装失败"
        exit 1
    fi
}
if [ ! -f "${RESOURCE}/gcc-5.3.0.tar.gz" ]; then
    echo "没有gcc-5.3.0.tar.gz资源"
    exit 1
else
    if [ ! -d "${RESOURCE}/gcc-5.3.0" ]; then
        cd ${RESOURCE}
        tar -zxvf gcc-5.3.0.tar.gz
    fi
    buildGcc
fi
